package com.zr.dao.impl;
import java.util.List;

import org.hibernate.Query;
import org.springframework.stereotype.Repository;

import com.zr.dao.OrdersDao;
import com.zr.model.Orders;

@Repository("orderDao")
public class OrdersDaoImpl<T> extends  BaseDaOImpl<T> implements OrdersDao<T> {

	@Override
	public boolean removeOrder(Integer oid, String ostatus) {
		Orders order =(Orders) this.getCurrentSession().get(Orders.class, oid);
		order.setOrderstatus(ostatus);	
		return true;
	}

	@Override
	public List<Orders> findOrder(Orders order,String strfhdate,String  overfhdate) {
		String sql="select * from orders as o  Where ((:place = 1) or (o.place=:place))"
				+ "and ((:receipt = 1) or(o.receipt=:receipt))"
				+ "and (o.sendname like :sendname)"
				+ "and ((:orderstatus = 1) or(o.orderstatus=:orderstatus))"
				+ "and ((:type = 1) or(o.type=:type))"
				+ "and ((:paystate = 1) or(o.paystate=:paystate))"
				+ "and ((:strdate = 1) or (o.fhdate >= :strdate))"
				+ "and ((:overdate = 1) or (o.fhdate <= :overdate))";
		Query  query  =  this.getCurrentSession().createSQLQuery(sql).addEntity(Orders.class);
	
		query.setString("place", order.getPlace());		
		query.setString("receipt", order.getReceipt());	
		query.setString("sendname", "%"+order.getSendname()+"%");
		query.setString("orderstatus", order.getOrderstatus());
		query.setString("type", order.getType());
		query.setString("paystate", order.getPaystate());
		query.setString("strdate", strfhdate);
		query.setString("overdate", overfhdate);
		List<Orders>  obs = query.list();
		System.out.println(obs.size());
		return obs;
	}

	@Override
	public boolean updateOrderState(Orders order) {
		Orders o =	(Orders) this.getCurrentSession().get(Orders.class,Integer.valueOf(order.getOid())); 
		o.setPaystate(order.getPaystate());
		return true;
	}

	@Override
	public int updateAndAddorder(Orders order) {
		Orders o = (Orders) this.getCurrentSession().get(Orders.class,Integer.valueOf(order.getOid()));
		o.setOrderstatus("已退订");
		o.setRemarks("退单");
		order.setOid(null);
		order.setAddressname(o.getSendname());
		order.setAddresstel(o.getSendtel());
		order.setReceipt(o.getPlace());
		
		order.setPlace(o.getReceipt());
		order.setPaystate("未支付");
		order.setOrderstatus("已处理");
		order.setRemarks("订单"+o.getOid()+"退订返回单");
		this.getCurrentSession().saveOrUpdate(order);
	
		return order.getOid();
	}

}
